package com.throttle.raildroid;
import java.sql.Time;
import java.util.Date;

import com.throttle.raildroid.ImageMap;
import com.throttle.raildroid.ImageMap.Area;
import com.throttle.raildroid.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;

public class RunActivity extends Activity {
	ImageMap mImageMap;
	Bitmap off;
	Bitmap on;
	Bitmap none;
	boolean listening = false;  
	
	public class flipListener implements Runnable{
		String state;
		Area a;
		flipListener(Area a){
			this.a = a;
		}
		public void run() {
			// TODO Auto-generated method stub
			Date cur = new Date();
			Date lat = new Date();
			long startTime = cur.getTime();
			int i = 0;
			while(lat.getTime() - startTime < 5000)
			{
				if (i%2==0)
				{
					this.a.setBitmap(on);
				}else
				{
					this.a.setBitmap(off);
				}
				i++;
				lat = new Date();
			}
			
			if (this.a.getValue("state")=="off"){
				this.a.setBitmap(on);
				this.a.addValue("state", "on");
			}else{
				this.a.setBitmap(off);
				this.a.addValue("state", "off");
			}
		}
	}
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.run);
        off = BitmapFactory.decodeResource(getResources(),R.drawable.off);
        on = BitmapFactory.decodeResource(getResources(),R.drawable.on);
        none = BitmapFactory.decodeResource(getResources(),R.drawable.none);
        // find the image map in the view
        mImageMap = (ImageMap)findViewById(R.id.map);
        StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"MapSetup."));
        for(Area a: mImageMap.mAreaList){
        	a.setBitmap(none);
        	a.addValue("state", "none");
        }
        
        // add a click handler to react when areas are tapped
        mImageMap.addOnImageMapClickedHandler(new ImageMap.OnImageMapClickedHandler() {
			public void onImageMapClicked(int id) {
				StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Trying to flip switch."));
				Area a = mImageMap.mIdToArea.get(id);
				boolean state = true;
				if (a.getValue("state")=="off"){
					a.setBitmap(on);
					a.addValue("state", "on");
					state=false;
				}else{
					a.setBitmap(off);
					a.addValue("state", "off");
				}
				boolean success = StaticLogList.throttle.setSwitchState(Integer.parseInt(a.getName())-1,state);
				if(success){
					StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Switch "+a.getName()+" flipped."));
					Toast.makeText(RunActivity.this, "Switch "+a.getName()+" flipped.", Toast.LENGTH_SHORT).show();
					return;
				}
				StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Switch "+a.getName()+" failed to flip."));
				a.setBitmap(none);
	        	a.addValue("state", "none");
				Toast.makeText(RunActivity.this, "Switch Failed to flip."+a.getName(), Toast.LENGTH_SHORT).show();
			}
			public void onBubbleClicked(int id) {
				// react to info bubble for area being tapped 
			}
		});
    }
}